package com.atguigu.qqzone.service.impl;

import com.atguigu.qqzone.dao.FruitDAO;
import com.atguigu.qqzone.pojo.Fruit;
import com.atguigu.qqzone.service.FruitServiceInterface;
import com.atguigu.uils.DruidUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @ClassName: FruitServiceImpl
 * @Description:
 * @Author: wty
 * @Date: 2022/12/8
 */

public class FruitServiceImpl implements FruitServiceInterface {
    //private FruitDAO fruitDAO = new FruitDAO();
    private FruitDAO fruitDAO = null;

    /**
     * @param
     * @return java.util.List<com.atguigu.fruit.pojo.Fruit>
     * @description 支持模糊和分页查询
     * @param: pageNumber
     * @param: keyword
     * @date 2022/12/8 0:24
     * @author wty
     **/
    @Override
    public List<Fruit> getFruitList(Integer pageNumber, String keyword) {
        System.out.println("getFruitList中的connection：" + DruidUtils.getConnection());
        String sqlStr = "select * from t_fruit where fname like ? or remark like ? limit ?,5";
        return fruitDAO.queryMulti(sqlStr, Fruit.class, "%" + keyword + "%", "%" + keyword + "%", (pageNumber - 1) * 5);
    }

    /**
     * @param
     * @return boolean
     * @description 添加库存记录
     * @param: fruit
     * @date 2022/12/8 0:24
     * @author wty
     **/
    @Override
    public boolean addFruit(Fruit fruit) {
        String sqlStr = "insert into t_fruit values(0,?,?,?,?)";
        int affectedrows = fruitDAO.dml(sqlStr, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());

        return affectedrows > 0;
    }

    /**
     * @param
     * @return com.atguigu.fruit.pojo.Fruit
     * @description 通过fid获取一行库存记录
     * @param: fid
     * @date 2022/12/8 0:23
     * @author wty
     **/
    @Override
    public Fruit getFruitByFid(Integer fid) {
        String sqlStr = "select * from t_fruit where fid = ?";
        return fruitDAO.querySingle(sqlStr, Fruit.class, fid);
    }

    /**
     * @param
     * @return java.lang.Integer
     * @description 获取总页数pageCount
     * @param: keyword
     * @date 2022/12/8 0:23
     * @author wty
     **/
    @Override
    public Integer getFruitCount(String keyword) {
        Connection connection = DruidUtils.getConnection();
        System.out.println("getFruitCount的connection是" + connection);
        String sqlStr = "select count(*) from t_fruit where fname like ? or remark like ?";
        Object o = fruitDAO.queryScalar(sqlStr, "%" + keyword + "%", "%" + keyword + "%");

        Integer i = 0;
        if (o instanceof Long) {
            i = ((Long) o).intValue();
        }
        Integer pageCount = (i + 5 - 1) / 5;

        return pageCount;
    }

    /**
     * @param
     * @return boolean
     * @description 通过fid删除某行记录
     * @param: fid
     * @date 2022/12/8 0:32
     * @author wty
     **/
    @Override
    public boolean delFruitByFid(Integer fid) {
        String sqlStr = "delete from t_fruit where fid = ?";
        int affectedrows = fruitDAO.dml(sqlStr, fid);
        return affectedrows > 0;
    }

    /**
     * @param
     * @return boolean
     * @description 修改水果库存记录
     * @param: fruit
     * @date 2022/12/8 0:32
     * @author wty
     **/
    @Override
    public boolean updateFruitByFid(Fruit fruit) {
        String sqlStr = "update t_fruit set fname = ?, price = ?, fcount = ?, remark = ? where fid = ? ";
        int affectedrows = fruitDAO.dml(sqlStr, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark(), fruit.getFid());
        return affectedrows > 0;
    }
}
